PCRE - Negative Lookbehind Assertion problem
PCRE - Negative Lookbehind Assertion problem
am 29.10.2007 21:49:40 von Jim
Hi,
I'm trying to prefix the "src" attribute of all "img" elements with a
given string, $prefix. Here's what I've got:
preg_replace('/\/', '
$1src="' . $prefix . '$2"$3/>', $content);
The problem comes in that it always performs the replace, even when
there's an "http" in the source attribute.
Here's the source for a quick test if anyone's interested:
$prefix = 'http://mydomain.com/';
$content = <<
EOT;
echo preg_replace('/\/', '
$1src="' . $prefix . '$2"$3/>', $content);
/*
* First img element's src attribute should be changed, but not the
second's.
*/
?>
Thanks to all,
Jim.
Re: PCRE - Negative Lookbehind Assertion problem
am 29.10.2007 22:05:41 von Jim
I just spotted one error, the equals sign after the negative
lookbehind, although that seems to have made little difference.
Updated code follows:
$prefix = 'http://mydomain.com/';
$content = <<
EOT;
echo preg_replace('/\/', '
$1src="' . $prefix . '$2"$3/>', $content);
/*
* First img element's src attribute should be
* changed, but not the second's.
*/
?>
Re: PCRE - Negative Lookbehind Assertion problem
am 29.10.2007 22:38:32 von luiheidsgoeroe
On Mon, 29 Oct 2007 22:05:41 +0100, Jim wrote:
> I just spotted one error, the equals sign after the negative
> lookbehind, although that seems to have made little difference.
> Updated code follows:
>
>
>
> $prefix =3D 'http://mydomain.com/';
>
> $content =3D <<
>
>
> Image" />
> EOT;
>
> echo preg_replace('/\/', '
> $1src=3D"' . $prefix . '$2"$3/>', $content);
>
> /*
> * First img element's src attribute should be
> * changed, but not the second's.
> */
>
> ?>
Well, taking a guess here, but 'http://' is not preceded by 'http', so i=
t =
will match in $2. Lookaheads/behinds without a literal match is often no=
t =
a very nice way to go. I'd use the preg_replace_callback or the /e =
modifier to let a function examine the match and act apropriately.
-- =
Rik Wasmus
Re: PCRE - Negative Lookbehind Assertion problem
am 29.10.2007 23:14:52 von Jim
> Well, taking a guess here, but 'http://' is not preceded by 'http', so it
> will match in $2. Lookaheads/behinds without a literal match is often not
> a very nice way to go.
Now I'm really confused, I thought that was the idea of the lookbehind
assertion, oh well.
> I'd use the preg_replace_callback or the /e
> modifier to let a function examine the match and act apropriately.
I'll give that route a try.
Thanks for your help Rik.
Jim.
Re: PCRE - Negative Lookbehind Assertion problem
am 29.10.2007 23:39:41 von luiheidsgoeroe
On Mon, 29 Oct 2007 23:14:52 +0100, Jim wrote:
>
>> Well, taking a guess here, but 'http://' is not preceded by 'http', so
>> it
>> will match in $2. Lookaheads/behinds without a literal match is often
>> not
>> a very nice way to go.
>
> Now I'm really confused, I thought that was the idea of the lookbehind
> assertion, oh well.
Think of it like this:
'foofoo' is foo before foo.
'foobar' is foo without foo in front of it.
With a literal match, I mean the following: if you know the text you are
looking for ('foo'), you can specify that it should or should not be
preceded by something ('bar') in order to match. However, if you don't
know the text you're looking for ((.*?)), you can specify all you want it
shouldn't be preceded by something, but text isn't preceded by itself.
--
Rik Wasmus